package ar.com.solidussource.howmuchservices.web.controller;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import ar.com.solidussource.framework.backend.dao.filter.Page;
import ar.com.solidussource.framework.backend.dao.filter.Result;
import ar.com.solidussource.framework.backend.dao.filter.TableOrder;
import ar.com.solidussource.howmuchservices.backend.bo.ProvinceCRUDBO;
import ar.com.solidussource.howmuchservices.common.entities.Province;
import ar.com.solidussource.howmuchservices.common.entities.filter.ProvinceFilter;
import ar.com.solidussource.howmuchservices.utils.json.JSONUtils;

@Controller
@RequestMapping("/province")
public class ProvinceController extends BaseController {
	private static final Logger log = Logger.getLogger(ProvinceController.class);
	
	private static final String TABLEORDER_CRITERION = "name";
	private static final String TABLEORDER_DIRECTION = "desc";
	
	@Autowired ProvinceCRUDBO provinceCRUDBO; 	
	
	@RequestMapping(value="/list.json", method = RequestMethod.GET, produces="application/json")
	@ResponseBody
	public String getProvinces(HttpServletResponse httpServletResponse, @RequestParam Integer pageNumber, @RequestParam Integer size) {
		
		try {
			ProvinceFilter provinceFilter = new ProvinceFilter();
			provinceFilter.setTableOrder(new TableOrder(TABLEORDER_CRITERION, TABLEORDER_DIRECTION));
			
			Page page = new Page(pageNumber, size);
			
			Result<Province> provinces = provinceCRUDBO.getAll(provinceFilter, page);
			
			return JSONUtils.getJSON(provinces.getResult());
			
		} catch (Exception e) {
			httpServletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			log.error(e.getMessage(), e);			
			return getJsonStatus(e.getMessage());
		}
 
	}	

}
